﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace 葵花宝典BookDemo.Views
{
    /// <summary>
    /// Chapter04Window.xaml 的交互逻辑
    /// </summary>
    public partial class Chapter04Window : Window
    {
        public Chapter04Window()
        {
            InitializeComponent();
        }
    }

    [ContentProperty("Text")]
    public class Book
    {
        public Book()
        {

        }

        public string Name { get; set; }


        public MoneyType Price { get; set; }

        public string Text { get; set; }

        public override string ToString()
        {
            return $"{Name}的价格是:{Price},{Text}";
        }

    }

    [TypeConverter(typeof(MoneyConventer))]
    public class MoneyType
    {
        private double _value;
        public MoneyType()
        {
            _value = 0;
        }

        public MoneyType(double value)
        {
            _value = value;
        }

        public override string ToString()
        {
            return _value.ToString();
        }

        public static MoneyType Parse(string value)
        {
            string str = (value as string).Trim();
            if (str[0] == '$')
            {
                string newPrice = str.Remove(0, 1);
                double price = double.Parse(newPrice);
                return new MoneyType(price * 8);
            }
            else
            {
                double price = double.Parse(str);
                return new MoneyType(price);
            }
        }

    }

    public class MoneyConventer : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
        {
            if (sourceType == typeof(string)) return true;
            return base.CanConvertFrom(context, sourceType);
        }

        public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
        {
            if (destinationType == typeof(string)) return true;
            return base.CanConvertTo(context, destinationType);
        }

        public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
        {
            if (value.GetType() != typeof(string))
            {
                return base.ConvertFrom(context, culture, value);

            }
            return MoneyType.Parse(value as string);
        }

        override public object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                return base.ConvertTo(context, culture, value, destinationType);
            }
            return value.ToString();
        }

    }

}
